package com.lijinjiang.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName Demo04
 * @Description TODO
 * @Author Li
 * @Date 2022/9/24 20:29
 * @ModifyDate 2022/9/24 20:29
 * @Version 1.0
 */
@WebServlet("/demo04")
public class Demo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // GET请求逻辑
        // System.out.println("Request Demo04 doGet...");
        // 1.获取所有参数的 Map 集合
        Map<String, String[]> map = req.getParameterMap();
        for (String key : map.keySet()) {
            System.out.print(key + ":");
            String[] values = map.get(key);
            for (String value : values) {
                System.out.print(value + " ");
            }
            System.out.println();
        }

        System.out.println("--------------------");

        // 2.根据 key 获取单个参数值
        String username = req.getParameter("username");
        System.out.println("username:" + username);
        String password = req.getParameter("password");
        System.out.println("password:" + password);

        System.out.println("--------------------");

        // 3.根据 key 获取参数值，数组
        String[] hobbies = req.getParameterValues("hobby");
        System.out.print("hobby:");
        for (String hobby : hobbies) {
            System.out.print(hobby + " ");
        }
        System.out.println();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
        /*// POST请求逻辑
        System.out.println("Request Demo04 doPost...");
        // 1.获取所有参数的 Map 集合
        Map<String, String[]> map = req.getParameterMap();
        for (String key : map.keySet()) {
            System.out.print(key + ":");
            String[] values = map.get(key);
            for (String value : values) {
                System.out.print(value + " ");
            }
            System.out.println();
        }

        System.out.println("--------------------");

        // 2.根据 key 获取单个参数值
        String username = req.getParameter("username");
        System.out.println("username:" + username);
        String password = req.getParameter("password");
        System.out.println("password:" + password);

        System.out.println("--------------------");

        // 3.根据 key 获取参数值，数组
        String[] hobbies = req.getParameterValues("hobby");
        System.out.print("hobby:");
        for (String hobby : hobbies) {
            System.out.print(hobby + " ");
        }
        System.out.println();*/
    }
}
